﻿using System;
using System.Collections.Generic;
using System.Data;
using Raven.Client;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Guia3.Models;

namespace Guia3.Controllers
{ 
    public class VagasController : Controller
    {
        private IVagaRepositorio vagayRepositorio;

        public VagasController(IVagaRepositorio vagayRepositorio)
        {
            this.vagayRepositorio = vagayRepositorio;
        }
        //
        // GET: /Vagas/

        public ActionResult Index()
        {
            var vagas = vagayRepositorio.GetVagas();
            if (vagas == null)
                return RedirectToAction("Create");
            return View(vagas);
        }
            

        //
        // GET: /Vagas/Details/5

        public ViewResult Details(string id)
        {
            Vaga vaga = vagayRepositorio.Load(id);
            return View(vaga);
        }

        //
        // GET: /Vagas/Create

        public ActionResult Create()
        {
            var vaga = new Vaga();
            return View("Save", vaga);
        } 

        //
        // POST: /Vagas/Create

        [HttpPost]
        public ActionResult Save(Vaga vaga)
        {
            if (ModelState.IsValid)
            {
                return View("Save", vaga);  
            }
            vagayRepositorio.Save(vaga);
            return RedirectToAction("Index");
        }
        
        //
        // GET: /Vagas/Edit/5
 
        public ActionResult Edit(string id)
        {
            var vaga = vagayRepositorio.Load(id);
            return View("Save", vaga);
        }

        // POST: /Vagas/Delete/5

        [HttpPost]
        public ActionResult Delete(string id)
        {            
            vagayRepositorio.Delete(id);
            var vagas = vagayRepositorio.GetVagas();
            return PartialView("VagaList", vagas);
        
        }

        protected override void Dispose(bool disposing)
        {
            Dispose();
            base.Dispose(disposing);
        }

        
    }
}